package com.cloudbroker.bcs.common.jres.service;

import java.lang.reflect.Method;
import java.util.Map;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.common.vo.HSBCSVO;
import org.slf4j.MDC;

import com.alibaba.fastjson.JSONException;
import com.cloudbroker.bcs.common.constants.BasicErrorCodes;
import com.cloudbroker.bcs.common.constants.LogFields;
import com.cloudbroker.bcs.common.jres.util.JresUtil;
import com.hundsun.jresplus.remoting.base.ServiceContext;

public abstract class T2ServiceAPIAdapterBase<T> extends
		MethodNameAccessServiceAdapterBase<T> {

	@SuppressWarnings("unchecked")
	protected Map<String, Object> callAPI2(String methodName,
								   HSBCSVO paramBean) {
		// 获取方法
		Method method = getMethod(methodName);
		try {
			String functionId = (String) ServiceContext.getCurrent().get(
					"functionId");
			if (null != functionId) {
				MDC.put(LogFields.FUNCTION_ID, functionId);
				// 注意，这里没做clear
				// 当前是放在com.cloudbroker.bcs.common.aspect.LogAspect.log(ProceedingJoinPoint)中clear
			}
			// 调用接口方法
			Map<String, Object> resultMap = (Map<String, Object>) method
					.invoke(getTargetService(), paramBean);
			return resultMap;
		} catch (Exception e) {
			throw new RuntimeException("call api method error", e);
		}
	}

	@SuppressWarnings("unchecked")
	protected Map<String, Object> callAPI(String methodName,
			Map<String, Object> param) {
		// 获取方法
		Method method = getMethod(methodName);
		try {
			String functionId = (String) ServiceContext.getCurrent().get(
					"functionId");
			if (null != functionId) {
				MDC.put(LogFields.FUNCTION_ID, functionId);
				// 注意，这里没做clear
				// 当前是放在com.cloudbroker.bcs.common.aspect.LogAspect.log(ProceedingJoinPoint)中clear
			}
			// 获取参数类型
			Class<?> paramType = method.getParameterTypes()[0];
			// map转bean
			Object paramBean = null;
			try {
				paramBean = JresUtil.bodyProtocolToBean(param, paramType);
			} catch (JSONException e) {
				return JresUtil.bodyProtocolToJresMap(VOUtil.genErrorResult(
						BasicErrorCodes.PARAM_ERROR, "参数错误:" + e.getMessage()));
			}
			// 调用接口方法
			Map<String, Object> resultMap = (Map<String, Object>) method
					.invoke(getTargetService(), paramBean);
			return resultMap;
		} catch (Exception e) {
			throw new RuntimeException("call api method error", e);
		}
	}
	@SuppressWarnings("unchecked")
	protected HSBCPSBackVO callAPI(String methodName,
								   HSBCSVO paramBean) {
		// 获取方法
		Method method = getMethod(methodName);
		try {
			String functionId = (String) ServiceContext.getCurrent().get(
					"functionId");
			if (null != functionId) {
				MDC.put(LogFields.FUNCTION_ID, functionId);
				// 注意，这里没做clear
				// 当前是放在com.cloudbroker.bcs.common.aspect.LogAspect.log(ProceedingJoinPoint)中clear
			}
			// 调用接口方法
			HSBCPSBackVO resultVo = (HSBCPSBackVO) method
					.invoke(getTargetService(), paramBean);
			return resultVo;
		} catch (Exception e) {
			throw new RuntimeException("call api method error", e);
		}
	}
}
